<?php
namespace Admin\Controller;
use Think\Controller;
class ClubController extends Controller {
	public function clublist(){
		$clubModel=M('club_info');
		$club=$clubModel->order('club_id asc')->select();
		//dump($club);
		$this->club=$club;
		$this->display();
	}
	public function addClub(){
		$this->display();

	}
	public function do_addClub(){
		$data=$_POST;
		$file=$_FILES;
		$clubModel=D('club_info');
		$createresult=$clubModel->create();
		if($createresult){
			$clubModel->club_name=trim(I('post.club_name'));
			$clubModel->club_tel=trim(I('post.club_intro'));
			$clubModel->club_address=trim(I('post.club_address'));
			$clubModel->club_time=date("Y-m-d H:i:s");
			if($file['club_photo']['size']==0){
				$clubModel->club_photo="nopic.jpg";

			}else{
			$info=$this->MyUpload();
			$clubModel->club_photo=$info['club_photo']['savename'];
         
     }  $addresult=$clubModel->add();

      if (false !== $addresult)
                 {
                    $this->success('添加成功！', '../Club/clublist');
                }else{ 
                   $this->error('数据错误'); 
               }


     } else{
            $this->error($clubModel->getError()); 	
	}
}
	public function deleteClub(){
		$club_id=I('club_id');
		$clubModel=M('club_info');
		$club=$clubModel->where("club_id=$club_id");
		$result=$clubModel->where("club_id=$club_id")->delete();
		$picPath='Public/Uploads/clubphoto/'.$club[0]['club_photo'];
		unlink($picPath);
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}

	}
	public function editClub(){
		$club_id=I('club_id');
		$clubModel=M('club_info');
		$club=$clubModel->where("club_id=$club_id")->select();
		$this->club=$club;
		$this->display();

	}
	public function updateClub(){
		$data=$_POST;
		$file=$_FILES;
		//dump($file);
		$club_id=$data['club_id'];
		//dump($club_id);
		$clubModel=M('club_info');
		if($file['club_photo']['size']==0){
			$result=$clubModel->where("club_id=$club_id")->save($data);
		}else{
			//修改头像
			$upload= new \Think\Upload();//实例化上传类
			$upload->maxSize = 3145728 ;
			  $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/clubphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
				 //保存表单数据 包括附件数据
               	$data['club_photo']= $info['club_photo']['savename']; 
                $result=$clubModel->where("club_id=$club_id")->save($data);
                	if ($data['oldpic']!='nopic.jpg') {
                	$oldpicPath='Public/Uploads/'.$data['oldpic'];
                	unlink($oldpicPath);
               		 	}
            		}
        	}
        	//dump($data);
		if($result){
       		 $this->success('修改成功！','../Club/clublist');
     		 }
      		else{
        		$this->error('修改失败！');
      			}

		

	}
	    public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/clubphoto/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                              // dump($info);    
                             //dump($info['photo']['savename']);
                             // 保存表单数据 包括附件数据
                         //处理头像图片
                        $picinfo="Public/Uploads/clubphoto/".$info['club_photo']['savename'];
                        $image = new \Think\Image(); 
                        $image->open($picinfo);
                // 生成一个居中裁剪为150*150的缩略图并保存
                        $image->thumb(150, 150,\Think\Image::IMAGE_THUMB_CENTER)->save($picinfo);
                        $image->open($picinfo)->text('AppStore','Public/assets_a/font/jinglei.ttf',20,'#000000',\Think\Image::IMAGE_WATER_SOUTHEAST)->save($picinfo); 
                        return $info;
                    }
                } 



}
?>